NewsPortlet = Class.extend({
	init: function()	{
		this.name = "NewsPortlet";
		this.page = 0;
		this.page_size = 30;
	},
	
	onBegin: function()	{
		this.model = {};
	},

	run: function()	{
		var obj = this;
		this.onAjax('news', 'get-news-list', {page:this.page,page_size:this.page_size}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = {};
				obj.model.newslist = ret;
				for(var i=0;i<obj.model.newslist.length;i++) {
					if (obj.model.newslist[i].content != null)
						obj.model.newslist[i].content = obj.model.newslist[i].content.substr(0, 300)+'...';
				}
				obj.getPortletPlaceholder().paintCanvas(obj.render());
			}
		});
	},
	
	onMore: function(){
		this.page++;
		this.run();
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface);

NewsDetailPortlet = Class.extend({
	init: function()	{
		this.name = "NewsDetailPortlet";
		this.page = 0;
		this.page_size = 30;
	},
	
	onBegin: function()	{
		this.model = {};
	},

	run: function()	{
		var obj = this;
		var id = this.getRequest().getParam('id');
		this.onAjax('news', 'get-news-detail', {id:id}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.getPortletPlaceholder().paintCanvas(obj.render());
			}
		});
	},
	
	onMore: function(){
		this.page++;
		this.run();
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface);

NewsManagePortlet = Class.extend({
	init: function()	{
		this.name = "NewsManagePortlet";
		this.page = 0;
		this.page_size = 30;
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	onReloadPage: function()	{
		this.run();
	},
	
	onNewsChanged: function()	{
		this.run();
	},
	
	run: function()	{
		this.loadNewsList();
	},
	
	loadNewsList: function()	{
		var obj = this;
		this.onAjax('news', 'get-news-list', {page:this.page,page_size:this.page_size}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = {};
				obj.model.newslist = ret;
				obj.getPortletPlaceholder().paintCanvas(obj.render());
			}
		});
	},
	
	onDeleteNews: function(eventData)	{
		var event = eventData.event;
		var id = eventData.id;
		var obj = this;
		this.onAjax('admin', 'delete-news', {id: id}, 'POST', {
			'onSuccess': function(ret)	{
				obj.run();
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);


NewsAddPortlet = Class.extend({
	init: function()	{
		this.name = "NewsAddPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	run: function()	{
		var obj = this;
		this.getPortletPlaceholder().paintCanvas(this.render());
		var subject = SingletonFactory.getInstance(Subject);
		subject.notifyEvent('RenderTextEditor', {id: 'NewsAddPortlet-Content', content: ''});
	},
	
	onNewsAddSubmit: function()	{
		var news_title=this.requestForEffectiveResource('Title').val();
		if (news_title.trim() == '')	{
			alert('Bạn chưa nhập tiêu đề tin');
			return;
		}
		var subject = SingletonFactory.getInstance(Subject);
		var obj = this;
		var content = '';
		subject.notifyEvent('RetrieveTextEditorContent', {id: 'NewsAddPortlet-Content', callback: function(new_content) {
			content = new_content;
		}});
		
		this.onAjax('admin', 'add-news', {news_title: news_title, content: content}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Thêm tin mới thành công');
				obj.requestForEffectiveResource('Title').val('');
				obj.requestForEffectiveResource('Content').val('');
				subject.notifyEvent('NewsChanged');
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);

NewsEditPortlet = Class.extend({
	init: function()	{
		this.name = "NewsEditPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	run: function()	{
		var obj = this;
		var id = this.getRequest().getParam('id');
		this.onAjax('news', 'get-news-detail', {id:id}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.getPortletPlaceholder().paintCanvas(obj.render());
				var subject = SingletonFactory.getInstance(Subject);
				subject.notifyEvent('RenderTextEditor', {id: 'NewsEditPortlet-Content', content: ''});
			}
		});
	},
	
	onNewsEditSubmit: function()	{
		var news_title=this.requestForEffectiveResource('Title').val();
		if (news_title.trim() == '')	{
			alert('Bạn chưa nhập tiêu đề tin');
			return;
		}
		var subject = SingletonFactory.getInstance(Subject);
		var obj = this;
		var content = '';
		subject.notifyEvent('RetrieveTextEditorContent', {id: 'NewsEditPortlet-Content', callback: function(new_content) {
			content = new_content;
		}});
		var id = this.getRequest().getParam('id');
		this.onAjax('admin', 'update-news', {id:id, news_title: news_title, content: content}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Cập nhật tin thành công');
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);